package com.feng.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.reggie.common.R;
import com.feng.reggie.entity.Orders;
import com.feng.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
@RequestMapping("/order")

public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 后台订单明细的分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number,String beginTime,String endTime){
        //分页构造器
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //构造条件查询对象
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件  动态sql  字符串使用StringUtils.isNotEmpty这个方法来判断
        //这里使用了范围查询的动态SQL，这里是重点！！
        queryWrapper.like(number!=null,Orders::getNumber,number)
                .gt(!StringUtils.isEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(!StringUtils.isEmpty(beginTime),Orders::getOrderTime,endTime);

        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);

    }

    /**
     *用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders,session);
        return R.success("下单成功！");
    }
}
